package com.rich.dmr.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rich.dmr.DmrApplication;
import com.rich.dmr.R;
import com.rich.dmr.ui.view.SimpleToolbar;
import com.rich.dmr.utils.Constant;

import rich.dmr.DmrManager;

public class SettingsActivity extends BaseActivity implements View.OnClickListener {
    private String TAG = "rich---SettingsActivity";
    private TextView tvShowId;
    private Intent intent = new Intent();
    private SharedPreferences mSp;
    private boolean isShowId;
    private DmrManager dmrManager;
    private int mDevHitCountdown;
    private Toast mDevHitToast;
    private static final int TAPS_TO_BE_A_DEVELOPER = 10;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        mSp = getSharedPreferences(Constant.sp_key, Context.MODE_PRIVATE);
        SimpleToolbar simpleToolbar = findViewById(R.id.settings_tools_bar);
        simpleToolbar.setLeftTitleClickListener(this);
        simpleToolbar.setLeftTitleDrawable(R.mipmap.nav_back_icon);
        simpleToolbar.setMainTitle(getString(R.string.tools_settings));

        LinearLayout showName = findViewById(R.id.settings_show_name);
        showName.setOnClickListener(this);

        tvShowId = findViewById(R.id.tv_show_name);

        LinearLayout dmrMode = findViewById(R.id.settings_mode_set);
        dmrMode.setOnClickListener(this);

        LinearLayout digitalMode = findViewById(R.id.settings_digit_set);
        digitalMode.setOnClickListener(this);

        LinearLayout about = findViewById(R.id.settings_about_set);
        about.setOnClickListener(this);

        isShowId = mSp.getBoolean(Constant.showLocalId, true);

        if (isShowId) {
            tvShowId.setBackgroundResource(R.drawable.shape_online);
        } else {
            tvShowId.setBackgroundResource(R.drawable.shape_setting_offline);
        }
        dmrManager = (DmrManager) getSystemService("dmrservice");
        mDevHitCountdown = TAPS_TO_BE_A_DEVELOPER;
        mDevHitToast = null;
        DmrApplication.getInstance().addActivity(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_left_title:
                finish();
                break;
            case R.id.settings_show_name:
                if (isShowId) {
                    tvShowId.setBackgroundResource(R.drawable.shape_setting_offline);
                } else {
                    tvShowId.setBackgroundResource(R.drawable.shape_online);
                }
                isShowId = !isShowId;
                SharedPreferences.Editor mEditor = mSp.edit();
                mEditor.putBoolean(Constant.showLocalId, isShowId);
                boolean commit = mEditor.commit();
                break;
            case R.id.settings_mode_set:
                intent.setClass(this, DmrModeSetActivity.class);
                startActivity(intent);
                break;
            case R.id.settings_digit_set:
                intent.setClass(this, DmrDigitalModeActivity.class);
                startActivity(intent);
                break;
            case R.id.settings_about_set:
                //bfp remove
                ///activeSysWriterFrequency();
                break;
            default:
                break;
        }
    }

    public void activeSysWriterFrequency() {
        Log.d(TAG, "mDevHitCountdown=" + mDevHitCountdown);
        if (mDevHitCountdown > 0) {
            mDevHitCountdown--;
            if (mDevHitCountdown == 0) {
                // Add 1 count back, then start password confirmation flow.
                mDevHitCountdown++;
                mDevHitCountdown = 0;
                if (mDevHitToast != null) {
                    mDevHitToast.cancel();
                }
                dmrManager.openOrCloseAdb(true);
                dmrManager.skipAdbConfirm(true);
                mDevHitToast = Toast.makeText(this, "write number mode open.",
                        Toast.LENGTH_LONG);
                mDevHitToast.show();
            } else if (mDevHitCountdown > 0
                    && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER - 2)) {
                if (mDevHitToast != null) {
                    mDevHitToast.cancel();
                }
                //mDevHitToast = Toast.makeText(this, +mDevHitCountdown + "", Toast.LENGTH_SHORT);
                //mDevHitToast.show();
            }

        } else {
            if (mDevHitToast != null) {
                mDevHitToast.cancel();
            }
            mDevHitToast = Toast.makeText(this, "write number mode open.",
                    Toast.LENGTH_LONG);
            mDevHitToast.show();
        }
    }
}
